package com.hj.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hj.model.AdvanceResponse;
import com.hj.model.User;
import com.hj.service.UserService;
import com.hj.util.RedisUtil;
import com.hj.utils.VerCodeGenerateUtil;
import com.hj.utils.VerCodeImageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 和用户有关的controller
 *
 * @author haoshen
 * @version 1.0
 */
@RestController
@RequestMapping("/user")
@SuppressWarnings("all")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private JavaMailSenderImpl mailSender;
    @Value("${spring.mail.username}")
    private String sender;

    /**
     * @return 全部用户的  List
     */
    @GetMapping("/listUser")
    public AdvanceResponse selAll(@RequestParam(required = true, defaultValue = "1") Integer num,
                                  @RequestParam(required = true, defaultValue = "5") Integer size,
                                  @RequestParam(required = true, defaultValue = "") String name) {
        PageHelper.startPage(num, size);
        List<User> all = userService.getAll(name);
        PageInfo<User> userpageInfo = new PageInfo<>(all);
        return AdvanceResponse.successResponse(userpageInfo);
    }

    /**
     * 生成图形验证码
     */
    @GetMapping("/imageCode")
    public AdvanceResponse imageCode(HttpServletResponse response, HttpServletRequest request) {
        try {
            VerCodeImageUtil.createImage(response);
            RedisUtil.set(request.getRemoteAddr(), VerCodeImageUtil.getCode(), 3, TimeUnit.MINUTES);
        } catch (IOException e) {
            e.printStackTrace();
            return AdvanceResponse.failedResponse("生成失败，请重试！");
        }
        return AdvanceResponse.successResponse();
    }

    /**
     * 登陆验证
     *
     * @param username 用户名
     * @param password 密码
     * @param request  request对象
     * @return
     */
    @PostMapping("/login")
    public AdvanceResponse login(String username, String password, String userCode, HttpServletRequest request) {
        return userService.login(username, password, request.getSession(), RedisUtil.get(request.getRemoteAddr()), userCode);
    }

    /**
     * 由id删除用户  支持多个一起删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/user")
    public AdvanceResponse delUser(@RequestBody List<Integer> ids, HttpServletRequest request) {
        return userService.delUser(ids, request);
    }

    /**
     * 由cookie中存储的name 得到用户  并存储在session中
     */
    @GetMapping("/getUser")
    public AdvanceResponse getUser(String name, HttpServletRequest request) {
        return userService.getUser(name, request.getSession());
    }

    /**
     * 发送验证码到指定邮箱
     */
    @GetMapping("/verCode")
    public AdvanceResponse verCode(String receiver, HttpServletRequest request) {
        String verCode = VerCodeGenerateUtil.generateVerCode();
        System.out.println(verCode);
        RedisUtil.set(receiver, verCode, 5, TimeUnit.MINUTES);
        return userService.getCode(sender, mailSender, receiver, verCode);
    }

    /**
     * 注册用户
     */
    @PostMapping("/addUser")
    public AdvanceResponse addUser(User user, String sender, JavaMailSenderImpl mailSender, @RequestParam("verCode") String verCode) {
        String code = (String) RedisUtil.get(user.getEmail());
        System.out.println(code);
        System.out.println(verCode.equals(code));
        return userService.addUser(user, verCode, code);
    }

    /**
     * 查看用户详情
     *
     * @param id 如果传了id  按id查找用户  否则按session中存储的用户
     */
    @GetMapping("/detail")
    public AdvanceResponse userDetail(HttpServletRequest request, Integer id) {
        return userService.userDetail(request, id);
    }

    /**
     * 修改用户信息
     */
    @PostMapping("/changeUser")
    public AdvanceResponse changeUser(User user) {
        return userService.changeUser(user);
    }

    /**
     * 查看用户名是否已经存在
     */
    @GetMapping("/isNameExist")
    public AdvanceResponse isNameExist(String name) {
        return userService.isNameExist(name);
    }

    /**
     * 修改用户密码
     */
    @PostMapping("/changePsw")
    public AdvanceResponse changePsw(User user, HttpServletRequest request) {
        return userService.changePsw(user, request);
    }
}
